package com.zhuangjie.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuangjie.base.constant.CommonSeparatorConstants;
import com.zhuangjie.base.entity.TimeRange;
import com.zhuangjie.shop.entity.ShopBusinessRules;
import com.zhuangjie.shop.enums.ShopBusinessRuleType;
import com.zhuangjie.shop.helper.ShopModuleTokenDataProvider;
import com.zhuangjie.shop.mapper.ShopBusinessRulesMapper;
import com.zhuangjie.shop.service.ShopBusinessRulesService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuangjie
 * @since 2023-11-16
 */
@Service
public class ShopBusinessRulesServiceImpl extends ServiceImpl<ShopBusinessRulesMapper, ShopBusinessRules> implements ShopBusinessRulesService {

    @Override
    public List<ShopBusinessRules> getShopBusinessRules(Long shopId) {
        return this.baseMapper.getShopBusinessRules(shopId);
    }

    @Override
    public List<ShopBusinessRules> listShopRules() {
        LambdaQueryWrapper<ShopBusinessRules> queryShopBusinessRules = Wrappers.lambdaQuery(ShopBusinessRules.class)
                .eq(ShopBusinessRules::getShopId, ShopModuleTokenDataProvider.getShopId());
        return this.list(queryShopBusinessRules);
    }

    @Override
    public void saveOrUpdateShopBusinessTimeRule(ShopBusinessRules shopBusinessRules) {
        // 1. 检查业务参数的有效性
        checkParameterValidityForBusiness(shopBusinessRules);
        // 2. 更新或保存
        // 2.1 保证是给自己添加的规则
        Long shopId = ShopModuleTokenDataProvider.getShopId();
        shopBusinessRules.setShopId(shopId);
        // 2.2 保存或更新
        this.saveOrUpdate(shopBusinessRules);
    }

    @Override
    public ShopBusinessRules getShopBusinessRuleById(Long ruleId) {
        LambdaQueryWrapper<ShopBusinessRules> queryShopBusinessRuleWrapper = Wrappers.lambdaQuery(ShopBusinessRules.class)
                .eq(ShopBusinessRules::getId, ruleId)
                .eq(ShopBusinessRules::getShopId, ShopModuleTokenDataProvider.getShopId());
        ShopBusinessRules shopBusinessRules = this.getOne(queryShopBusinessRuleWrapper);
        return shopBusinessRules;
    }

    @Override
    public void removeShopBusinessRuleById(Long ruleId) {
        LambdaQueryWrapper<ShopBusinessRules> queryShopBusinessRuleWrapper = Wrappers.lambdaQuery(ShopBusinessRules.class)
                .eq(ShopBusinessRules::getId, ruleId)
                .eq(ShopBusinessRules::getShopId, ShopModuleTokenDataProvider.getShopId());
        boolean isRemoveSuccess = this.remove(queryShopBusinessRuleWrapper);
        if (! isRemoveSuccess) {
            throw new IllegalArgumentException("删除失败");
        }
    }

    private void checkParameterValidityForBusiness(ShopBusinessRules shopBusinessRules) {
        if (shopBusinessRules.getType().equals(ShopBusinessRuleType.WEEK_TIMES.getCode())) {
            // 普通类型-weeks必须有值
            if (shopBusinessRules.getWeeks() == null || shopBusinessRules.getWeeks().isEmpty()) {
                throw new IllegalArgumentException("周时间段不能为空");
            }
        }else if (shopBusinessRules.getType().equals(ShopBusinessRuleType.DATE_TIMES.getCode())) {
            // 特殊类型-startDate必须有值
            if (shopBusinessRules.getStartDate() == null) {
                throw new IllegalArgumentException("开始日期不能为空");
            }
            // 特殊类型-endDate必须有值
            if (shopBusinessRules.getEndDate() == null) {
                throw new IllegalArgumentException("结束日期不能为空");
            }
            // 还需要检查结束时间是否在开始时间之后
            if (shopBusinessRules.getEndDate().isBefore(shopBusinessRules.getStartDate())) {
                throw new IllegalArgumentException("结束日期必须在开始日期之后");
            }
        }else {
            throw new IllegalArgumentException("规则类型不正确");
        }
        // 检查times是否为”12:30-14:30,17:30-18:00“格式
        for (String timeRangeString : shopBusinessRules.getTimes().split(CommonSeparatorConstants.MULTIPLE_SEPARATOR)) {
            if (!TimeRange.isValidTimeRange(timeRangeString, CommonSeparatorConstants.TIME_RANGE_SEPARATOR)) {
                throw new IllegalArgumentException("时间范围格式不正确！");
            }
        }
    }


}
